package com.xiaoshuidi.cloud.module.finance.bwclient;

import com.dtflys.forest.http.ForestRequest;
import com.dtflys.forest.lifecycles.MethodAnnotationLifeCycle;
import com.dtflys.forest.reflection.ForestMethod;
import com.xiaoshuidi.cloud.module.finance.util.invoice.HtjsApiSdkClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;


@Component
@Slf4j
public class GatewayAuthLifeCycle implements MethodAnnotationLifeCycle<GatewayAuth, Object> {

    /**
     * 网关KeyId
     */
    private String accessKeyId = "网关KeyId，申请后提供";

    /**
     * 网关Secret
     */
    private String accessKeySecret = "网关Secret 申请后提供";

    @Override
    public boolean beforeExecute(ForestRequest request) {

        HtjsApiSdkClient hsc = new HtjsApiSdkClient(accessKeyId, accessKeySecret);
        try {
            Map header = hsc.get_signed_headers("POST", request.getUrl());
            request.addHeader(header);
            return true;
        } catch (Exception e) {
            log.error(e.getMessage());
            return false;
        }
    }

    @Override
    public void onMethodInitialized(ForestMethod method,GatewayAuth annotation) {

    }
}
